package com.ruoyi.web.controller.ring.app;

import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.Builder;
import com.ruoyi.ring.domain.UserPhysiology;
import com.ruoyi.ring.domain.UserPhysiologySet;
import com.ruoyi.ring.domain.dto.PhysiologyInfoDTO;
import com.ruoyi.ring.domain.dto.PhysiologyInfoQueryDTO;
import com.ruoyi.ring.service.IUserPhysiologyService;
import com.ruoyi.ring.service.IUserPhysiologySetService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 生理期信息 - APP
 * 
 * @author liuwp
 * @date 2025-09-30
 */
@Slf4j
@RestController
@RequestMapping("/ring/app/user_physiology")
public class AppUserPhysiologyController extends BaseController
{
    @Resource
    private IUserPhysiologyService userPhysiologyService;
    @Resource
    private IUserPhysiologySetService userPhysiologySetService;
    /**
     * 获取生理期信息详细信息 - APP
     */
    @GetMapping(value = "/query-physiology-info")
    public AjaxResult queryPhysiologyInfo(PhysiologyInfoQueryDTO queryDTO) {
        long userId = SecurityUtils.getUserId();
        log.info("获取生理期信息详细信息: userId={}, param={}", userId, JSON.toJSONString(queryDTO));
        queryDTO.setUserId(userId);
        return AjaxResult.success(userPhysiologyService.queryPhysiologyInfo(queryDTO));
    }
    /**
     * 新增或修改生理期信息 - APP
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/add-or-edit")
    public AjaxResult addOrEdit(@RequestBody PhysiologyInfoDTO physiologyInfoDTO) {
        long userId = SecurityUtils.getUserId();
        log.info("新增或修改生理期信息: userId={}, param={}", userId, JSON.toJSONString(physiologyInfoDTO));
        UserPhysiology physiology = Convert.convert(UserPhysiology.class,physiologyInfoDTO);
        UserPhysiologySet physiologySet = userPhysiologySetService.selectOneUserPhysiologySet(Builder.of(UserPhysiologySet::new).with(UserPhysiologySet::setUserId, userId).build());
        if(physiology.getUpdStartDate() != null){
            physiology.setStartDate(physiology.getUpdStartDate());
            physiologySet.setStartDate(physiology.getUpdStartDate());
        }else {
            physiology.setStartDate(physiologySet.getStartDate());
            physiologySet.setStartDate(DateUtils.queryMonthOfYear(physiologySet.getStartDate(),Convert.toInt(physiologySet.getPeriodDays())));
        }
        userPhysiologySetService.updateUserPhysiologySet(physiologySet);

        UserPhysiology userPhysiology = userPhysiologyService.queryOneUserPhysiology();
        if(userPhysiology != null){
            physiology.setId(userPhysiology.getId());
            return toAjax(userPhysiologyService.updateUserPhysiology(physiology));
        }

        physiology.setPeriodId(physiologySet.getId());
        physiology.setUserId(userId);
        physiology.setMenstrualPeriodDays(physiologySet.getMenstrualPeriodDays());
        physiology.setPeriodDays(physiologySet.getPeriodDays());
        return toAjax(userPhysiologyService.insertUserPhysiology(physiology));
    }

}
